package com.yanqu.road.server.cmd.rank;

import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.config.vehicle.VehicleInfo;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.logic.pb.RankPb;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.config.VehicleMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/9/4
 */
@Cmd(code = GameProtocol.S_BIG_RANK_TYPE_TOP, desc = "按大类型取榜首")
public class GetRankTopByBigRankTypeCmd extends AbstractCommand {
    int bigRankType;
    int smallType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.GetRankTopByTypeReqMsg msg = RankProto.GetRankTopByTypeReqMsg.parseFrom(packet.getBytes());
        bigRankType = msg.getBigRankType();
        smallType = msg.getSmallType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RankProto.GetRankTopByTypeRespMsg.Builder builder = RankProto.GetRankTopByTypeRespMsg.newBuilder();

        List<Integer> list = new ArrayList<>();
        if (bigRankType == eBigRankType.Skin.getValue()) {
            for (SkinInfo skinInfo : SkinMgr.getSkinInfoMap().values()) {
                if (skinInfo.getType() == smallType || smallType == 0) {
                    list.add(skinInfo.getSkinId());
                }
            }
        } else if (bigRankType == eBigRankType.Vehicle.getValue()) {
            for (VehicleInfo vehicleInfo : VehicleMgr.getVehicleInfoMap().values()) {
                if (VehicleMgr.canJoinRank(vehicleInfo.getId())) {
                    list.add(vehicleInfo.getId());
                }
            }
        } else if (bigRankType == eBigRankType.Patrons.getValue()) {
            for (PatronsInfo patronsInfo : PatronsMgr.getPatronsInfoMap().values()) {
                list.add(patronsInfo.getId());
            }
        } else if (bigRankType == eBigRankType.Beauty.getValue()){
            for (BeautyInfo beautyInfo : BeautyMgr.getBeautyInfoMap().values()) {
                list.add(beautyInfo.getBeautyId());
            }
        }

        // 判断是跨服的还是本服的
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        if(serverGroup != null && serverGroup.isCrossRank()){
            RankProto.CrossGroupRankTopByTypeReqMsg.Builder req = RankProto.CrossGroupRankTopByTypeReqMsg.newBuilder();
            req.setGroupId(serverGroup.getGroupId());
            req.setBigType(bigRankType);
            req.addAllType(list);
            player.sendPacket(CrossProtocol.C_BIG_RANK_TYPE_TOP,req);
            return;
           /* for (int skinId : list) {
                RankProto.RankListMsg.Builder rankListMsg = CrossDataMgr.getCrossGroupRankMsg(skinId, bigRankType);
                if(rankListMsg.getRankListCount() > 0) {
                    builder.addTop(RankPb.getTopRankTemp(skinId, rankListMsg.getRankList(0)));
                }
            }*/
        } else {
            Map<Integer, RankListModel> modelMap = RankMgr.getRankListModelMap(bigRankType);
            for (Map.Entry<Integer, RankListModel> entry : modelMap.entrySet()) {
                if (list.contains(entry.getKey())) {
                    if (entry.getValue() != null) {
                        RankProto.RankListMsg.Builder rankListMsg = entry.getValue().getRankListMsg();
                        if(rankListMsg.getRankListCount() > 0) {
                            builder.addTop(RankPb.getTopRankTemp(entry.getKey(), rankListMsg.getRankList(0)));
                        }
                    }
                }
            }
        }

        player.sendPacket(ClientProtocol.U_BIG_RANK_TYPE_TOP, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
